package com.xwj.config;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author xwj
 * @date 2020/12/15
 */
@Configuration
@EnableConfigurationProperties({DataSourceSecondProperties.class})
@MapperScan(basePackages = {"com.xwj.dao.db2"}, sqlSessionFactoryRef = "sqlSessionFactory2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DataSourceSecondConfig {
    @Bean("dataSource2")
    public DataSource dataSource2(DataSourceSecondProperties dataSourceSecondProperties) throws Exception {
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(dataSourceSecondProperties.getJdbcUrl());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXADataSource.setUser(dataSourceSecondProperties.getUsername());
        mysqlXADataSource.setPassword(dataSourceSecondProperties.getPassword());

        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        atomikosDataSourceBean.setUniqueResourceName("dataSource2");
        atomikosDataSourceBean.setMinPoolSize(dataSourceSecondProperties.getMinPoolSize());
        atomikosDataSourceBean.setMaxPoolSize(dataSourceSecondProperties.getMaxPoolSize());
        atomikosDataSourceBean.setMaxLifetime(dataSourceSecondProperties.getMaxLifetime());
        atomikosDataSourceBean.setBorrowConnectionTimeout(dataSourceSecondProperties.getBorrowConnectionTimeout());
        atomikosDataSourceBean.setLoginTimeout(dataSourceSecondProperties.getLoginTimeout());
        atomikosDataSourceBean.setMaintenanceInterval(dataSourceSecondProperties.getMaintenanceInterval());
        atomikosDataSourceBean.setMaxIdleTime(dataSourceSecondProperties.getMaxIdleTime());
        atomikosDataSourceBean.setTestQuery(dataSourceSecondProperties.getTestQuery());
        return atomikosDataSourceBean;
    }

    @Bean("sqlSessionFactory2")
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean("sqlSessionTemplate2")
    public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        return sqlSessionTemplate;
    }
}
